#!/bin/sh
#----------------------------------------------------------
# install moServices rev 0.5 and above (I'm hope)
#
if [ -f repo ] ; then
  repdir=$( cat repo )
else 
  repdir="http://mos-digma.googlecode.com/svn/trunk/packages/"
fi

clear
echo "==============================================="
echo "== Installing package of additional services =="
echo "== ----------------------------------------- =="
echo "== Version: 0.6 (tested on firmware 1.9.9)   =="
echo "== ----------------------------------------- =="
echo "== 23/06/10 > FarVoice  > farvoice@gmail.com =="
echo "== 25/11/10 > LinX                           =="
echo "==============================================="
echo "Choose an option:"
echo "=> (1) - Minimal installation(Core, RootApp and ftp)"
echo "=> (2) - Core only installation"
echo "=> (3) - Exit"
read option

if [ "$option" == "1" ] ; then
  fl=mos_min
elif [ "$option" == "2" ] ; then
  fl=mos_core
else
  exit 0;
fi

cd /tmp
if [ ! -f $fl.zip ]; then
  wget $repdir$fl.zip
  [ $? != 0 ] && exit 1
fi

# unpack files
if  [ ! -z $( unzip -qo $fl.zip ) ] ; then
  echo "Error of downloading file $fl.zip. Please, repeat install!"
  rm $fl.*
  exit 1
fi

# checking previous installation

mos=/usr/local/etc/mos

if [ -d $mos ] ; then
  echo "Remove previous installation..."
  $mos/services stop
  $mos/services disable
  chown -Rh root:root $mos
  rm -Rf /usr/local/etc/mos/*
fi

tar -xf $fl.tar -C /usr/local/
[ $? != 0 ] && exit 1

rm $fl.*

# change file attributes
chown -Rh root:root $mos
chmod +x $mos/services
#core
chmod +x $mos/scripts/core
chmod +x $mos/core/web/cgi-bin/services.cgi
#rootapp
chmod +x $mos/scripts/rootapp
chmod +x $mos/rootapp/RootApp
chmod +x $mos/rootapp/rc.suspend
chmod +x $mos/rootapp/rc.wakeup
#vsftpd
chmod +x $mos/scripts/vsftpd
chmod +x $mos/vsftpd/vsftpd

# starting core
$mos/services enable
$mos/services start

# need reboot...
echo "==============================================="
echo "The system needs to be restarted!"
echo "Enter (1) to restart HDMP now, or any key to restart manually later."
read option
      
if [ "$option" == "1" ] ; then
  echo "Restarting Digma HDMP. Wait a moment..."
  sync
  reboot
fi
